﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace ExcelJsonParser
{
    class JsonWorkSheets
    {
        List<JsonWorkSheet> _jsonWorkSheetList = new List<JsonWorkSheet>();
        public JsonWorkSheets(Sheets wss)
        {
            foreach (Worksheet ws in wss)
            {
                Add(ws);
            }
        }

        public JsonWorkSheet Add(Worksheet ws)
        {
            JsonWorkSheet jsonWs = null;
            if (TryGetWorkSheetByName(ws.Name, out jsonWs) == false)
            {
                jsonWs = new JsonWorkSheet(ws);
                _jsonWorkSheetList.Add(jsonWs);
            }
            return jsonWs;
        }

        public void Remove(Worksheet ws)
        {
            JsonWorkSheet jsonWs = null;
            if (TryGetWorkSheetByName(ws.Name, out jsonWs))
            {
                _jsonWorkSheetList.Remove(jsonWs);
            }
        }

        public Boolean TryGetWorkSheetByName(string name, out JsonWorkSheet jsonWorkSheet)
        {
            List<JsonWorkSheet> jsonWsList = new List<JsonWorkSheet>();
            foreach (JsonWorkSheet jsonWs in _jsonWorkSheetList)
            {
                if ("".Equals(jsonWs.Name))
                {
                    jsonWsList.Add(jsonWs);
                }
            }
            foreach (JsonWorkSheet jsonWs in jsonWsList)
            {
                _jsonWorkSheetList.Remove(jsonWs);
            }
            foreach (JsonWorkSheet jsonWs in _jsonWorkSheetList)
            {
                if (name.Equals(jsonWs.Name))
                {
                    jsonWorkSheet = jsonWs;
                    return true;
                }
            }
            jsonWorkSheet = null;
            return false;
        }
    }
}
